<template>
  <div id="rightLeft3" class="p-10">
    <div class="title p-lr-30">
      <div class="txt">这是渐变标题</div>
    </div>
    <div id="rightLeft3body"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "",
  components: {},
  data() {
    return {
      myChart: null,
    };
  },
  mounted() {
    this.setEcharts();
    window.addEventListener("resize", this.resize);
  },
  methods: {
    setEcharts() {
      let dataX = ["cq", "sh", "bj"];
      let dataY = [123, 234, 159];
      // 初始化
      let colorStopsArray = [
        [
          {
            offset: 0,
            color: "white",
            //  0%  处的颜色
          },
          {
            offset: 0.7,
            color: "#0471a9",
            //  100%  处的颜色
          },
          {
            offset: 1,
            color: "transparent",
            //  100%  处的颜色
          },
        ],
        [
          {
            offset: 0,
            color: "white",
            //  0%  处的颜色
          },
          {
            offset: 0.7,
            color: "#789810",
            //  100%  处的颜色
          },
          {
            offset: 1,
            color: "transparent",
            //  100%  处的颜色
          },
        ],
        [
          {
            offset: 0,
            color: "white",
            //  0%  处的颜色
          },
          {
            offset: 0.7,
            color: "#9d4c3e",
            //  100%  处的颜色
          },
          {
            offset: 1,
            color: "transparent",
            //  100%  处的颜色
          },
        ],
      ];

      this.myChart = echarts.init(document.getElementById("rightLeft3body"));
      var gray = "#9fa2ab";
      var option = {
        title: {
          text: "(单位:个)",
          x: "6%",
          y: "2%",
          textStyle: {
            color: gray,
            fontSize: 14,
            fontWeight: 400,
          },
        },
        backgroundColor: "transparent",
        grid: {
          left: "10%",
          top: "20%",
          bottom: "15%",
          right: "10%",
        },
        tooltip: {
          // trigger: 'axis',
          // axisPointer: {
          //   type: 'none',
          // },
          // extraCssText: 'width:100px;height:60px;'
        },
        xAxis: {
          data: dataX,
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "rgba(45, 140, 240, 0.65)",
              width: 1,
              //这里是为了突出显示加上的
            },
          },
          axisLabel: {
            show: true,
            interval: 0,
            //代表显示所有x轴标签显示
            rotate: 0,
            //代表逆时针旋转45度
            margin: 10,
            textStyle: {
              color: gray,
              fontSize: 14,
              fontWeight: 400,
            },
          },
        },
        yAxis: [
          {
            splitNumber: 2,
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "rgba(24,144,255, 0.15)",
                width: 1,
                //这里是为了突出显示加上的
              },
            },
            axisLabel: {
              // inside: true,
              textStyle: {
                color: gray,
                fontWeight: 400,
                fontSize: 14,
              },
            },
            splitArea: {
              show: false,
              areaStyle: {
                color: "rgba(255,255,255,.5)",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(24,144,255, 0.15)",
                width: 1,
                type: "solid",
              },
            },
          },
        ],
        series: [
          {
            name: "个数",
            type: "pictorialBar",
            // barCategoryGap: '-50%',
            symbol: "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z",
            label: {
              show: true,
              position: "top",
              distance: 15,
              color: "#278ec2",
              fontWeight: "bolder",
              fontSize: 14,
            },
            itemStyle: {
              normal: {
                color: function (data) {
                  var obj = {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [colorStopsArray[data.dataIndex][0], colorStopsArray[data.dataIndex][1], colorStopsArray[data.dataIndex][2]],
                    global: false,
                    //  缺省为  false
                  };

                  return obj;
                },
              },
              emphasis: {
                opacity: 1,
              },
            },
            data: dataY,
            z: 10,
          },
        ],
      };
      this.myChart.setOption(option);
    },
    resize() {
      // 屏幕尺寸改变重置echarts
      if (this.myChart) {
        this.myChart.resize();
      }
    },
  },
  beforeDestroy() {
    // 销毁实例
    echarts.dispose(this.myChart);
    this.myChart = null;
  },
  watch: {
    option(val, oldVal) {
      this.myChart.setOption(val);
    },
  },
};
</script>

<style scoped lang="scss">
#rightLeft3 {
  width: 100%;
  height: 100%;

  .title {
    background: url("@/assets/img/titleBg.png") no-repeat;
    height: 50px;
    width: 100%;

    background-size: 100% 100%;
    .txt {
      @extend .number;
    }
  }

  #rightLeft3body {
    width: 100%;
    height: calc(100% - 55px);
    background: url("@/assets/img/contentBg.png") no-repeat;
    background-size: 100% 100%;
  }
}
</style>
